part of common_lib_plugin;

extension DoubleExtension on double {
  String truncateAsString([int fractionDigits = 2]) {
    String numStr = toString();
    if (!numStr.contains('.')) return numStr;
    if (numStr.split('.')[1].length <= fractionDigits) return numStr;
    return numStr.substring(0, numStr.lastIndexOf('.') + fractionDigits + 1);
  }

  //保留几位小数
  String formatNum(int fractionDigits) {
    try {
      if ((toString().length - toString().lastIndexOf('.') - 1) < fractionDigits) {
        //小数点后有几位小数
        return toStringAsFixed(fractionDigits).substring(0, toString().lastIndexOf('.') + fractionDigits + 1).toString();
      } else {
        return toString().substring(0, toString().lastIndexOf('.') + fractionDigits + 1).toString();
      }
    } catch (e) {
      logV('format number error: $e');
      return '0';
    }
  }

  BorderRadius circular() => BorderRadius.circular(this);

  BorderRadius vertical({
    double? top,
    double? bottom,
  }) =>
      BorderRadius.vertical(
        top: Radius.circular(top ?? this),
        bottom: Radius.circular(bottom ?? this),
      );

  BorderRadius horizontal({
    double? left,
    double? right,
  }) =>
      BorderRadius.horizontal(
        left: Radius.circular(left ?? this),
        right: Radius.circular(right ?? this),
      );

  TextStyle textStyle({FontWeight? fontWeight, double? fontHeight, double? fontSize, Color? color}) {
    return TextStyle(
      fontWeight: fontWeight,
      fontSize: this,
      height: fontHeight ?? 1.4,
      color: color,
    );
  }
}
